<?php
declare (strict_types=1);

namespace app\user\logic\user;

use app\user\model\user\Ticket as TicketModel;

/**
 * 活动逻辑层
 */
class Ticket extends TicketModel
{
    /**
     * 获取列表
     *
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map = [])
    {
        return $this->where($map)->order('create_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * getInfoById
     *
     * @param int $id
     * @return Ticket|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfoById(int $id)
    {
        $info = $this->where('id', $id)->find();
        if ($info && $info->pid > 0) {
            $info->title = $this->where('id', $info->pid)->value('title');
        }

        return $info;
    }

    /**
     * 搜索
     *
     * @param string $keyword
     * @param int    $limit
     * @return Ticket[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function search(string $keyword, int $limit = 15)
    {
        return $this->whereLike('title', '%' . $keyword . '%')->field(['title', 'id' => 'value'])->limit($limit)->select();
    }

}
